<?php
class TagsController extends AppController {

	var $name = 'Tags';
	var $uses = array('Tag');
	var $components = array('Seo');
	
	function beforeFilter() {
		parent::beforeFilter();
	}
	
	function admin_index() {
		$this->Tag->recursive = 1;
		$this->set('tags', $this->paginate());
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this -> admin_seo();
			$this->Tag->create();
			if ($this->Tag->save($this->data)) {
				$this->Session->setFlash(__('TAG_SAVED_OK',true));
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash(__('TAG_SAVED_ERROR',true));
			}
		}
	}

	function admin_edit($id = null) {
		if (!$this -> Tag -> findById($id)) {
			$this->Session->setFlash(__('TAG_INVALID',true));
			$this->redirect(array('action'=>'index'), null, true);
		}
		if (!empty($this->data)) {
			$this -> admin_seo();
			if ($this->Tag->save($this->data)) {
				$this->Session->setFlash(__('TAG_SAVED_OK',true));
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash(__('TAG_SAVED_ERROR',true));
			}
		} else {
			$this->data = $this->Tag->read(null, $id);
		}
	}

	function admin_delete($id = null) {
		if (!$this -> Tag -> findById($id)) {
			$this->Session->setFlash(__('TAG_INVALID',true));
		}
		if ($this->Tag->del($id)) {
			$this->Session->setFlash(__('TAG_DELETED_OK',true));
		} else {
			$this->Session->setFlash(__('TAG_DELETED_ERROR',true));
		}
		$this->redirect(array('action'=>'index'), null, true);
	}
	
	function admin_seo() {
		$this -> data['Tag']['seo_link'] = $this->Seo->create($this->data['Tag']['name']);
	}

	function admin_delete_tags() {
		if($this->Tag->deleteAll(array('Tag.id'=>$this->data['TagsDelete']))) {
			$this->Session->setFlash(__('TAGS_DELETED_OK',true));
		} else {
			$this->Session->setFlash(__('TAGS_DELETED_ERROR',true));
		}
		$this->redirect(array('action'=>'index'), null, true);
	}
	
	function index() {
		$this->Tag->recursive = 1;
		$this->set('tags', $this->paginate());
	}
	
	function view($seo = null) {
		if (!$this->Tag->findById(array('seo_link'=>$seo))) {
			$this->Session->setFlash(__('TAG_INVALID',true));
			$this->redirect(array('action'=>'index'), null, true);
		}
		$this->Tag->recursive = 2;
		$this->paginate = array('conditions'=>'seo_link="'.$seo.'"');
		$this->set('tag', $this->paginate());
	}
}
?>